package Servlets;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ServiceInterface.ServiceProxy;
import Beans. *;



/**
 * Servlet implementation class SearchForMovie
 */
public class SearchForMovie extends HttpServlet {
	private static final long serialVersionUID = 1L;
	ServiceProxy proxy = new ServiceProxy();
      
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchForMovie() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Movie theMovieToSearch = new Movie();
		List<Movie> movieList;
		try{
			theMovieToSearch.setBanner(request.getParameter("banner"));
			theMovieToSearch.setCategory(request.getParameter("category"));
			theMovieToSearch.setDate(request.getParameter("releasedate"));
			theMovieToSearch.setTitle(request.getParameter("title"));
			if(request.getParameter("id") != "")
				theMovieToSearch.setMovieID(Integer.parseInt(request.getParameter("id")));
			if(request.getParameter("quantity") != "")
				theMovieToSearch.setQuantity(Integer.parseInt(request.getParameter("quantity")));
			if(request.getParameter("fee") != "")
				theMovieToSearch.setRentalFee(Double.parseDouble(request.getParameter("fee")));
			
			proxy.setEndpoint("http://localhost:8079/VLMS3/services/Service");
			Movie[] moviesArray = proxy.searchMovie(theMovieToSearch);
			movieList = Arrays.asList(moviesArray);
			request.setAttribute("movies", movieList);
			request.getRequestDispatcher("ShowMovies").forward(request, response);


		}catch(Exception e){
			e.printStackTrace();
		}

		
	}

}
